package common;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.text.SimpleDateFormat;

public class DateCellRenderer extends DefaultTableCellRenderer {

    private SimpleDateFormat formatter;

    public DateCellRenderer(SimpleDateFormat formatter) {
        this.formatter = formatter;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        this.setFont((Font)null);
        if (value != null) {
            this.setText(formatter.format(value));
        } else {
            this.setText("");
        }

        if (isSelected) {
            this.setBackground(table.getSelectionBackground());
        } else {
            this.setBackground((Color)null);
        }

        this.setHorizontalAlignment(SwingConstants.CENTER);

        return this;
    }
}
